package com.sktlab.bizconfmobile.model.manager;

import java.util.Comparator;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;

import android.database.SQLException;

import com.sktlab.bizconfmobile.model.ConferenceHistory;
import com.sktlab.bizconfmobile.model.db.HistoryDBAdapter;
import com.sktlab.bizconfmobile.model.db.HistoryDbTable;

public class ConferenceHistoryManager {
	public static final int ALL_WEEKS = 52;
	public static final String OLDER = "DOlder";
	public static final String ONE_WEEK_AGO = "BOne Week Ago";
	public static final String THIS_WEEK = "AThis Week";
	public static final String TWO_WEEKS_AGO = "CTwo Weeks Ago";
	private static ConferenceHistoryManager m_instance = null;
	private TreeSet<ConferenceHistory> m_history = new TreeSet(
			new HistoryComparator());
	private HistoryDBAdapter m_historyDBHelper = new HistoryDBAdapter(
			"history", HistoryDbTable.getAllColumns());
	private TreeMap<String, TreeSet<ConferenceHistory>> m_historyNow = new TreeMap();
	private Date m_lastDateNow = null;
	private String m_lastSearchedString = new String();

	ConferenceHistoryManager() {
		loadHistory(null);
	}

	private void addOrInsertHistory(String paramString,
			ConferenceHistory paramConferenceHistory) {
		if (paramConferenceHistory != null) {
			if (this.m_historyNow.containsKey(paramString))
				((TreeSet) this.m_historyNow.get(paramString))
						.add(paramConferenceHistory);
		} else
			return;
		TreeSet localTreeSet = new TreeSet(new HistoryComparator());
		localTreeSet.add(paramConferenceHistory);
		this.m_historyNow.put(paramString, localTreeSet);
	}

	private boolean checkHistoryExistance(
			ConferenceHistory paramConferenceHistory) {
		boolean isExist = this.m_history.contains(paramConferenceHistory);

		return isExist;
	}

	public static ConferenceHistoryManager getInstance() {
		if (m_instance == null)
			m_instance = new ConferenceHistoryManager();
		return m_instance;
	}

	private String getKey(String paramString) {
		String str1 = new String();
		try {
			String str2 = paramString.substring(0, 1).toUpperCase();
			return str2;
		} catch (Exception localException) {
		}
		return str1;
	}

	private void insertRecordBasedOnWeek(int paramInt,
			ConferenceHistory paramConferenceHistory) {
		switch (paramInt) {
		default:
			addOrInsertHistory("DOlder", paramConferenceHistory);
			return;
		case 1:
			addOrInsertHistory("BOne Week Ago", paramConferenceHistory);
			return;
		case 2:
		}
		addOrInsertHistory("CTwo Weeks Ago", paramConferenceHistory);
	}

	private void removeHistoryFromNow(ConferenceHistory paramConferenceHistory) {
		if (this.m_historyNow.size() > 0) {
			Iterator localIterator = this.m_historyNow.entrySet().iterator();
			boolean bool;
			do {
				if (!localIterator.hasNext())
					break;
				Map.Entry localEntry = (Map.Entry) localIterator.next();
				TreeSet localTreeSet = (TreeSet) localEntry.getValue();
				bool = localTreeSet.remove(paramConferenceHistory);
				if (localTreeSet.size() != 0)
					continue;
				this.m_historyNow.remove(localEntry.getKey());
			} while (!bool);
		}
	}

	public long addHistory(ConferenceHistory paramConferenceHistory) {
		long historyId = -1L;

		if ((paramConferenceHistory != null)
				&& (!checkHistoryExistance(paramConferenceHistory))) {

		} else {

			return historyId;
		}

		try {
			this.m_historyDBHelper.open();
			historyId = this.m_historyDBHelper
					.insertObject(paramConferenceHistory);

			if (historyId != -1L) {
				paramConferenceHistory.setHistoryId(historyId);
				this.m_history.add(paramConferenceHistory);
			}
			return historyId;
		} catch (SQLException localSQLException) {
			return historyId;
		} finally {
			this.m_historyDBHelper.close();
		}
	}

	public boolean clearHistory() {
		return true;
	}

	public TreeSet<ConferenceHistory> getHistory() {
		return this.m_history;
	}

	public ConferenceHistory getHistoryById(long paramLong) {
		Iterator localIterator = this.m_history.iterator();
		ConferenceHistory localHistory;
		while (true) {
			boolean bool = localIterator.hasNext();
			localHistory = null;
			if (!bool)
				break;
			ConferenceHistory localConferenceHistory = (ConferenceHistory) localIterator
					.next();
			if (localConferenceHistory.getHistoryId() != paramLong)
				continue;
			localHistory = localConferenceHistory;
		}
		return localHistory;
	}

	public TreeMap<String, TreeSet<ConferenceHistory>> getHistoryFromNow() {
		int i = new GregorianCalendar().get(3);
		GregorianCalendar localGregorianCalendar = new GregorianCalendar();
		Iterator localIterator = this.m_history.iterator();
		while (localIterator.hasNext()) {
			ConferenceHistory localConferenceHistory = (ConferenceHistory) localIterator
					.next();
			localGregorianCalendar.setTime(localConferenceHistory
					.getStartDate());
			int j = localGregorianCalendar.get(3);
			if (i == j) {
				addOrInsertHistory("AThis Week", localConferenceHistory);
				continue;
			}
			int k = i - j;
			if (k > 0) {
				insertRecordBasedOnWeek(k, localConferenceHistory);
				continue;
			}
			insertRecordBasedOnWeek(k + 52, localConferenceHistory);
		}
		return this.m_historyNow;
	}

	public void loadHistory(String paramString) throws SQLException {

	}

	public boolean removeHistory(long paramLong) {
		Object localHistory = new ConferenceHistory();

		if (paramLong < 0) {

			return false;
		}

		boolean isDelSuccess = false;
		try {
			this.m_historyDBHelper.open();
			isDelSuccess = this.m_historyDBHelper.deleteObject(paramLong);
			if (isDelSuccess) {
				Iterator localIterator = this.m_history.iterator();
				while (localIterator.hasNext()) {
					ConferenceHistory localConferenceHistory = (ConferenceHistory) localIterator
							.next();
					if (localConferenceHistory.getHistoryId() != paramLong)
						continue;
					localHistory = localConferenceHistory;
				}
				this.m_history.remove(localHistory);
				removeHistoryFromNow((ConferenceHistory) localHistory);
			}
			return isDelSuccess;
		} catch (SQLException localSQLException) {
			return isDelSuccess;
		} finally {
			this.m_historyDBHelper.close();
		}

	}

	public class HistoryComparator implements Comparator<ConferenceHistory> {
		public HistoryComparator() {
		}

		public int compare(ConferenceHistory paramConferenceHistory1,
				ConferenceHistory paramConferenceHistory2) {
			Date localDate1 = paramConferenceHistory1.getStartDate();
			Date localDate2 = paramConferenceHistory2.getStartDate();
			if (paramConferenceHistory1.equals(paramConferenceHistory2))
				return 0;
			return localDate2.compareTo(localDate1);
		}
	}
}
